package com.ideabove.adapter;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;

import com.ideabove.network.NetWorkManager;
import com.ideabove.radio.MainActivity;
import com.ideabove.radio.R;
import com.ideabove.radio.module.FacebookMd;
import com.ideabove.radio.module.FolderEditMd;
import com.ideabove.radio.service.PlayBackService;
import com.ideabove.radio.service.PlayBackService.MediaPlayerSingleton;
import com.ideabove.util.ConnectThread;
import com.ideabove.util.img.ImageLoader;
import com.ideabove.custom.MyCropImageView;
import com.ideabove.data.Channel;
import com.ideabove.data.Folder;
import com.ideabove.data.PlayItem;
import com.ideabove.data.Song;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.os.Message;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ScaleXSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import com.ideabove.util.img.BitmapDownloaderTask;

public class YtSongAdapter extends BaseAdapter
{

	private List<PlayItem> mList;
	private LayoutInflater mInflater;  
	private Context context;
	private MainActivity activity;
	private boolean isEditable = false; // 1: edit mode
	private int index = 0;
	private int animation = 0;
	
	

	public YtSongAdapter(Context c , MainActivity activty, ArrayList<PlayItem> arrays){
		this.mInflater = LayoutInflater.from(c);
		this.mList = arrays;
		this.context = c;
		this.activity = activty;
	}

	public void setEditMode(boolean isEditable){
		this.isEditable = isEditable;
	}
	
	public void setAnimation(int ani){
		this.animation = ani;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{

		if(convertView == null)
		{
			convertView = mInflater.inflate(R.layout.row_ytsong, null);			
		}

		final PlayItem song = mList.get(position);
		if (song != null) {
			MyCropImageView songImg = (MyCropImageView) convertView.findViewById(R.id.ytsong_img);
			songImg.setImageResource(R.drawable.black);
			songImg.setScaleType(ScaleType.FIT_XY);
			songImg.setImageUrl(song.getThumbUrl());

			songImg.setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {

					if(event.getAction() == MotionEvent.ACTION_UP){
						
						// 음악을 틀어주자...
						Thread temp = new Thread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								Message msg = Message.obtain(null, PlayBackService.REQUEST_MSG_ADD_TEMP_PLAYITEM_PLAY);							
								msg.obj = song;
								activity.sendMessageToService( msg);
							}
						});
						temp.setPriority(Thread.MAX_PRIORITY);
						temp.start();
						
						activity.songSearchMd.hide();					
					}
					return true;
				}
			});

			TextView songTitle = (TextView) convertView.findViewById(R.id.ytsong_title);
			songTitle.setText(song.getTitle());

			TextView ytsong_view_cnt = (TextView) convertView.findViewById(R.id.ytsong_view_cnt);
			ytsong_view_cnt.setText(song.getViewCnt());	

			
			// 선택버튼
			ImageView btn_song_choice = (ImageView) convertView.findViewById(R.id.btn_song_choice);
			btn_song_choice.setTag(song);
			btn_song_choice.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {

					if(event.getAction() == MotionEvent.ACTION_UP){
						activity.songInsertMd.showSongInsert((PlayItem)v.getTag(), null);
					}
					return true;
				}
			});
		}

		if(this.animation > 0){
//			Animation animation = AnimationUtils.loadAnimation(activity, R.anim.slide_in_right);
			Animation animation = AnimationUtils.loadAnimation(activity, this.animation);
			convertView.startAnimation(animation);
		}
		


		return convertView;
	}

	@Override
	public int getCount() {
		return mList.size();
	}
	@Override
	public PlayItem getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
